<%@page language="java" pageEncoding="utf-8"%>
<%@page import="inet.util.DateProc"%>
<%@page import="java.util.Vector"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="inet.util.StringTool"%>
<%@page import="lib.db.clip.ClipCategory"%>
<%@page import="java.util.Iterator"%>
<%@page import="lib.db.clip.Clip"%>
<%@page import="lib.db.member.User"%>
<%@page import="lib.util.EncrypterStandard"%>
<jsp:useBean id="clipDAO" class="lib.db.clip.ClipDAO" scope="session"/>
<jsp:useBean id="userDAO" class="lib.db.member.UserDAO" scope="session"/>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Danh sách thành viên</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  	<link rel="icon" href="<%=request.getContextPath() %>/favicon.ico" type="image/x-icon" />
    <link href="../include/default.css" type="text/css" rel="stylesheet" />
	<script src="../include/default.js" type="text/javascript"></script>
	<link href="../include/gallery.css" rel="stylesheet" type="text/css" />
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/include/ui-lightness/jquery-ui-1.7.2.custom.css" />
	<script type="text/javascript" src="<%=request.getContextPath() %>/include/jquery-1.3.2.min.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath() %>/include/jquery-ui-1.7.2.custom.min.js"></script>
    <script type="text/javascript">
	  	function markAll(){
			var chkAll = document.frm_show.checkAllBox.checked;
			var field = document.frm_show.check_value;
			if(chkAll == true){
				for (i = 0; i < field.length; i++)
					field[i].checked = true ;
				field.checked = true;
			}else{
				for (i = 0; i < field.length; i++)
					field[i].checked = false ;
				field.checked = false;
			}
		}
	    function xoaAll(){
			var field = document.frm_show.check_value;
			var hasRow = false;
			for (i = 0; i < field.length; i++){
				if(field[i].checked == true)
					hasRow = true;
			}
			if(!hasRow && document.frm_show.check_value.checked){
				hasRow = true;
			}
			if(hasRow == false){
				alert('Chọn cột cần xóa');
				return false;
			}
			if(confirm('Bạn có chắc chắn xóa không?')){
				document.frm_show.action.value = 'DELETE_ALL';
				document.frm_show.submit();
			}
		}
	    function lockAll(){
			var field = document.frm_show.check_value;
			var hasRow = false;
			for (i = 0; i < field.length; i++){
				if(field[i].checked == true)
					hasRow = true;
			}
			if(!hasRow && document.frm_show.check_value.checked){
				hasRow = true;
			}
			if(hasRow == false){
				alert('Chọn cột cần lock');
				return false;
			}
			if(confirm('Bạn có chắc chắn LOCK không?')){
				document.frm_show.action.value = 'LOCK_ALL';
				document.frm_show.submit();
			}
		}
	    function unlockAll(){
			var field = document.frm_show.check_value;
			var hasRow = false;
			for (i = 0; i < field.length; i++){
				if(field[i].checked == true)
					hasRow = true;
			}
			if(!hasRow && document.frm_show.check_value.checked){
				hasRow = true;
			}
			if(hasRow == false){
				alert('Chọn cột cần unlock');
				return false;
			}
			if(confirm('Bạn có chắc chắn UNLOCK không?')){
				document.frm_show.action.value = 'UNLOCK_ALL';
				document.frm_show.submit();
			}
		}
		function showDialog(link){
			var html = ''+ link;
			$('#dialog-modal').dialog('open');
			$("#dialog-modal").dialog({
				width: 520,
				height: 150,
				modal: true,
			});    
			$('#dialog-modal').dialog('option', 'title', 'Nhúng link ảnh vào bài viết hoặc trang web');
			$('#dialog-modal').html('<textarea id="link_img" style="height: 50px; width: 480;" onclick="SelectAll(\'link_img\');">' + html + '</textarea>');
		}
		
		function showDialog1(link){
			var html = ''+ link;
			$('#dialog-modal1').dialog('open');
			$("#dialog-modal1").dialog({
				width: 720,
				height: 500,
				modal: true,
			});    
			$('#dialog-modal1').dialog('option', 'title', 'Preview ảnh');
			$('#dialog-modal1').html('<img style="height: 400px; width: 680px;" src="'+html+'" />');
		}
		function SelectAll(id)
		{
		    document.getElementById(id).focus();
		    document.getElementById(id).select();
		}
		
		function resetPass(id) {
			frm_show.id.value = id;
			frm_show.action.value = 'reset_pass';
			frm_show.submit();
		}
  	</script>
  	<style type="text/css">
  		.responderButton{
  			background-color: #5F4C0B; color: #fff; border: 1px solid #fff; 
  			border-radius: 5px; cursor: pointer; height: 25px;
  		}
  		input.reset-button {
		    border: 1px solid #CCCC99;
		    border-radius: 2px 2px 2px 2px;
		    color: #fff;
		    cursor: pointer;
		    font-size: 11px;
		    font-weight: bold;
		    height: 20px;
		    line-height: 20px;
		    margin: 0;
		    padding: 0 10px;
		    position: relative;
		    text-align: center;
		    vertical-align: middle;
		}
  		input.reset-button {
		    /*-moz-box-align: stretch;
		    -moz-box-sizing: content-box;
		    -moz-user-select: none;
		    */
		    background: -moz-linear-gradient(center top , #CCCC99, #CCCC99) repeat scroll 0 0 transparent;
		    
		    background-image: -webkit-gradient(
				linear,
				left bottom,
				left top,
				color-stop(0.13, #356acb),
				color-stop(0.68, #356acb)
			);
		    transition: all 0.2s ease 0s;
		}
  	</style>
  </head>
  <body marginheight="0" topmargin="0" marginwidth="0" leftmargin="0" rightmargin="0">
  	<%@ include file="../include/checklogin.jsp" %>
  	<%
  		request.setCharacterEncoding("utf-8");
  		String sAction = request.getParameter("action");
  		String msg = "";
  		if ("delete_all".equalsIgnoreCase(sAction)) {
			String[] variable = request.getParameterValues("check_value");
    		if(variable != null && variable.length > 0){
				for(int i = 0; i < variable.length;i++){
					//Kiểm tra landing_page_post có form_id = variable[i]
					//Danh sách người dùng đã đăng ký ? post_id --> form_id 
					if(userDAO.deleteRow(new BigDecimal(variable[i]))){
						msg = "<span style='color:blue'>Xóa Thành Viên thành công</span>";
					}else{
						msg = "<span style='color:red'>Xóa Thành Viên KHÔNG thành công</span>";
					}
				}
			}
		} else if ("lock_all".equalsIgnoreCase(sAction)) {
			String[] variable = request.getParameterValues("check_value");
    		if(variable != null && variable.length > 0){
				for(int i = 0; i < variable.length;i++){
					//Kiểm tra landing_page_post có form_id = variable[i]
					//Danh sách người dùng đã đăng ký ? post_id --> form_id 
					if(userDAO.updateStatus(new BigDecimal(variable[i]), User.USER_STATUS_DEACTIVE)){
						msg = "<span style='color:blue'>Khóa Thành Viên thành công</span>";
					}else{
						msg = "<span style='color:red'>Khóa Thành Viên KHÔNG thành công</span>";
					}
				}
			}
		} else if ("unlock_all".equalsIgnoreCase(sAction)) {
			String[] variable = request.getParameterValues("check_value");
    		if(variable != null && variable.length > 0){
				for(int i = 0; i < variable.length;i++){
					//Kiểm tra landing_page_post có form_id = variable[i]
					//Danh sách người dùng đã đăng ký ? post_id --> form_id 
					if(userDAO.updateStatus(new BigDecimal(variable[i]), User.USER_STATUS_ACTIVE)){
						msg = "<span style='color:blue'>Active Thành Viên thành công</span>";
					}else{
						msg = "<span style='color:red'>Active Thành Viên KHÔNG thành công</span>";
					}
				}
			}
		} else if ("reset_pass".equalsIgnoreCase(sAction)) {
			String sMemberId = request.getParameter("id");
			BigDecimal memberId = BigDecimal.ZERO;
			if(sMemberId != null && StringTool.isNumberic(sMemberId)) {
				memberId = new BigDecimal(sMemberId);
			}
			String newPassword = EncrypterStandard.md5Encrypt("123456");
			if(userDAO.updatePassword(memberId, newPassword)){
				msg = "<span style='color:blue'>Reset mật khẩu Thành Viên thành công (123456)</span>";
			}else{
				msg = "<span style='color:red'>Reset mật khẩu Thành Viên KHÔNG thành công</span>";
			}
		}
  	%>
  	<div class="list" style="margin-left: 16.5%; height: 30px; padding-top: 10px; font-size: 15px;">
  		<a href="<%=request.getContextPath() %>/">Home page</a> &raquo;
  		Danh sách thành viên
  	</div>
    <center>
  	<p align="center">
		<b><font color="#000080" face="Tahoma" size="4">
			QUẢN LÝ THÀNH VIÊN
		</font></b>
  	</p>
	<div align="center"><%=msg %></div>
	<form name="frm_show" method="post">
		<input type="hidden" name="action" />
		<input type="hidden" name="id" />
  	<% 
  		String sPage = request.getParameter("page");
    	String sCateId = request.getParameter("cateId");
    	BigDecimal cateId = BigDecimal.ZERO;
    	if(sCateId == null) sCateId = "-1";
    	if(sCateId != null && StringTool.isNumberic(sCateId)) {
    		cateId = new BigDecimal(sCateId);
    	}
    	
  	%>
  		<table width="66%" cellpadding="0" cellspacing="0" border="0" height="30px">
	  		<tr bgcolor="#EEEEEE" height="30px">
	  			<td align="center" class="header" width="3%">
		  			<input type="checkbox" name="checkAllBox" onClick="markAll();"/>
		  		</td>
		  		<td width="1px" bgcolor="#fff"></td>
		  		<td align="center" class="header">
		  			STT
		  		</td>	
		  		<td width="1px" bgcolor="#fff"></td>
		  		<td align="center" class="header">
		  			Số điện thoại
		  		</td>
		  		<td width="1px" bgcolor="#fff"></td>
		  		<td align="center" class="header">
		  			Họ và Tên 
		  		</td>
		  		<td width="1px" bgcolor="#fff"></td> 
		  		<td class="header" align="center">
		  			Ngày tạo
		  		</td>
		  		<td width="1px" bgcolor="#fff"></td> 
		  		<td class="header" align="center">
		  			Ngày hết hạn
		  		</td>
		  		<td width="1px" bgcolor="#fff"></td> 
		  		<td class="header" align="center">
		  			Trạng thái
		  		</td>
		  		<td width="1px" bgcolor="#fff"></td>
		  		<td class="header" align="center">
		  			Hành động
		  		</td>
	  		</tr>
   <%
	    if(sPage == null){
	    	sPage="1"; 
	    }
	    int currPage=1;
	    try{
	        currPage = Integer.parseInt(sPage);
	    }catch(NumberFormatException ex){
	    	currPage = 1;
	    }
	  	int PagesOfSegment = 5;
	    int rowsPerPage = 24;
		int totalRecs = 0;
		int numOfPages = 0;
		totalRecs = userDAO.countAll();
		
		numOfPages = (totalRecs % rowsPerPage != 0)?(totalRecs / rowsPerPage + 1):(totalRecs / rowsPerPage);
		if(totalRecs <= rowsPerPage) {
			numOfPages = 1;
		}
		if(totalRecs == 0){ 
			numOfPages = 1;
		}
		if(currPage > numOfPages) {
			currPage = numOfPages;
		}
		if(currPage < 0) {
			currPage = 1;
		}
		Vector vUsers = null;
		vUsers = userDAO.findAll(currPage, rowsPerPage);
		if( vUsers != null && !vUsers.isEmpty()) {
			User user = null;
			int i = 0;
			for( Iterator it = vUsers.iterator(); it.hasNext(); )  {
				user = (User) it.next();
				if(user == null) continue;
				i++;
   %>
		<tr height="30px">
 			<td  align="center" class="header">
	  			<input type="checkbox" name="check_value" value="<%= user.getId() %>" />
	  		</td>
	  		<td bgcolor="#fff"></td>
	  		<td class="list" align="center">
	  			<%=i + rowsPerPage * (currPage - 1) %>
	  		</td>
	  		<td bgcolor="#fff"></td>
	  		<td class="list">
	  			<%= user.getUsername() %>
	  		</td>
	  		<td bgcolor="#fff"></td>	
	  		<td align="center" class="list">
	  			<%= user.getFullname() != null ? user.getFullname():"" %>
	  		</td>
	  		<td bgcolor="#fff"></td>
	  		<td class="list" align="center" >
	  			<%=DateProc.Timestamp2DDMMYYYY(user.getGenDate()) %>
	  		</td>
	  		<td bgcolor="#fff"></td>
	  		<td class="list" align="center" >
	  			<%=DateProc.Timestamp2DDMMYYYY(user.getExpriedDate()) %>
	  		</td>
	  		<td bgcolor="#fff"></td>
	  		<td class="list" align="center" >
	  			<%= user.getStatus() == User.USER_STATUS_ACTIVE ? "Kích hoạt":"Ngưng hoạt động" %>
	  		</td>
	  		<td bgcolor="#fff"></td>
	  		<td class="list" align="center">
  				<input type="button" name="btnResetPass" value="Reset pass" class="reset-button GEE3RVNDNU" 
  					onclick="if(confirm('Bạn có chắc chắn muốn reset mật khẩu ???')){resetPass('<%= user.getId() %>');}"/>
	  		</td>
  		</tr>
   <%
   			} // end for
   		}
   %>	
   		</table>
		<table cellpadding="0" cellspacing="0" border="0" width="66%" style="margin-top: 10px;">
			<tr>
				<td>
					<input type="button" class="blogg-button GEE3RVNDNU" value="Xóa" onclick="return xoaAll();"/>
					&nbsp;&nbsp;
					<input type="button" class="blogg-button GEE3RVNDNU" value="Khóa" onclick="return lockAll();"/>
					&nbsp;&nbsp;
					<input type="button" class="blogg-button GEE3RVNDNU" value="Active" onclick="return unlockAll();"/>
					&nbsp;&nbsp;
				</td>
				<td class="list" colspan="10" align="right">
				<%
					int startNo = 0;
					int endNo = 0;
					if (numOfPages > 1) {
						//Print Previous Page if required
						out.println("Trang");
						int currSegment = currPage / PagesOfSegment;
						if (currPage % PagesOfSegment > 0)
							currSegment += 1;
						startNo = currSegment * PagesOfSegment - PagesOfSegment + 1;
						if (currSegment * PagesOfSegment > numOfPages)
							endNo = numOfPages;
						else
							endNo = currSegment * PagesOfSegment;
						if (currSegment > 1) {
							out.print("<a href='?page=1&cateId="+ sCateId +"' class='bluedark-text'> Đầu </a>");
							out.print("<a  href='?page=" + (currSegment - 1)
									* PagesOfSegment + "&cateId="+ sCateId +"' class='bluedark-text'> &lt;&lt;</a>");
						}
						//Print direct access page numbers.
						for (int No = startNo; No <= endNo; No++) {
							if (No == currPage) {
								out.print("<font color='red'><b>" + No + "</b></font>  ");
							} else {
								out.print("<a href='?page=" + No + "&cateId="+ sCateId +"' class='bluedark-text'>" + No + "</a>  ");
							}
						}
						//Print Next Page if required
						if (numOfPages != endNo)
							out.print(" ...<a href='?page=" + numOfPages
									+ "&cateId="+ sCateId +"' class='bluedark-text'> " + numOfPages + " </a>");
		
						if (numOfPages > PagesOfSegment * currSegment) {
							out.print("<a href='?page=" + ((currSegment * PagesOfSegment) + 1)
									+ "&cateId="+ sCateId +"' class='bluedark-text'>&gt;&gt;</a>");
							out.print("<a href='?page=" + numOfPages + "&cateId="+ sCateId +"' class='bluedark-text'> Cuối </a>");
						}
					}
				%>
				</td>
			</tr>
		</table>
		<div id="dialog-modal" title="Mã nhúng" style="display: none;"></div>	
		<div id="dialog-modal1" title="Mã nhúng" style="display: none;"></div>	
   	</form>
   </center>
  </body>
</html>
